package com.news.web.servlet.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.news.entity.Admin;
import com.news.service.AdminService;
import com.news.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AdminAdminInsertServlet
 */
@WebServlet("/AdminAdminInsertServlet")
public class AdminAdminInsertServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		// 获取传入数据
		String adminName = request.getParameter("adminName");
		String adminPass = request.getParameter("adminPass");

		
		// 检查 所有值 是否为空
		if (adminName == null || adminPass==null) {
			// 处理 值为空的情况
			System.out.println("Str is null");
			request.setAttribute("adminInsertBoolean", null); // 或者设定为null
			request.getRequestDispatcher("/admin/adminAdmin.jsp").forward(request, response);
			return; // 结束方法，避免继续执行下面的逻辑
		}

		

		// 实例化 admin 对象
		Admin admin = new Admin();
		admin.setAdminName(adminName);
		admin.setAdminPass(adminPass);

		// 创建 adminService 实例
		AdminService adminService = new AdminServiceImpl();

		// 执行添加方法
		Boolean adminInsertBoolean = adminService.insertAdmin(admin);
		
		//执行查询方法
		List<Admin> adminQueryLikeList = adminService.likeSelectListAdmin(admin);
	
		// 将数据存入 request 属性中
		request.setAttribute("adminQueryLikeList", adminQueryLikeList);
		request.setAttribute("adminInsertBoolean", adminInsertBoolean);

		// 跳转回原页面
		request.getRequestDispatcher("/admin/adminAdmin.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
